﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using KendoMvcUI.Common;

namespace KendoMvcUI.Core
{
    /// <summary>
    /// 控件设置基类
    /// </summary>
    public class ControlSettingsBase
    {
        [PropertySetting(IsIngoreForJavaScript=true)]
        public string Id { get; set; }
        public string Name { get; set; }
        public int? Height { get; set; }
        public int? Weight { get; set; }
        public string StyleClass { get; set; }
        
        public override string ToString()
        {
            
            return string.Empty;
        }
        /// <summary>
        /// 将设置类转换成js设置类格式，如：
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        public virtual string ToJsSettings()
        {
            Type type = this.GetType();
            string js = "";
            foreach (var property in type.GetProperties())        //获取所有的属性
            {
                string jsProperty = "";                           //js属性名
                string jsValue = "";                              //js属性值
                
                dynamic attrs = Attribute.GetCustomAttribute(property,typeof(PropertySettingAttribute));
                if (attrs != null)
                {
                    PropertySettingAttribute propertySetting = (PropertySettingAttribute)attrs;
                    if (propertySetting.IsIngoreForJavaScript)
                        continue;
                    jsProperty = propertySetting.PropertyName.LowercaseFirst();
                }
                    
                if (jsProperty.IsNullOrWhiteSpace())
                    jsProperty = property.Name.LowercaseFirst();
                if (jsValue.IsNullOrWhiteSpace())
                    jsValue = property.GetValue(this, null) == null ? "" : property.GetValue(this, null).ToString();

                if (!jsValue.IsNullOrWhiteSpace())           //如果属性值不为空，则显示
                {
                    if((property.PropertyType==typeof(string))
                        ||property.PropertyType.BaseType==typeof(Enum))
                    {
                        if (jsProperty.StartsWith("on_"))   //事件
                        {
                            jsProperty = jsProperty.Replace("on_", "").LowercaseFirst();
                            js += jsProperty + ":" + jsValue + ",";
                        }
                        else
                            js += jsProperty + ":'" + jsValue + "',";
                    }
                    else if(property.PropertyType==typeof(Nullable<bool>))
                        js += jsProperty + ":" + jsValue.LowercaseFirst() + ",";
                    else
                        js += jsProperty + ":" + jsValue + ",";
                }
            }
            return js.TrimEnd(',');
        }
        protected string AddOptions(string script,string options)
        {
            if (!script.EndsWith(","))
                script += ",";
            return script += options;
        }
    }
}
